package com.chat.datea.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.Fragment_Man_Singer_GridView_Adapter;
import com.chat.datea.adapter.Fragment_man_ListView_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.SingerEntity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.gridView.NoScrollGridView;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;


/**
 * Created by dllo on 15/10/26.
 * 点击男歌手跳转页面
 * 王锦予
 */
public class FragmentManSinger_jump extends BaseFragment implements View.OnClickListener {
    private ListView manSinger_listView;
    private NoScrollGridView manSinger_gridView;
    private Fragment_Man_Singer_GridView_Adapter gridViewAdapter;
    private Fragment_man_ListView_Adapter adapter;
    private VolleySingleton singleton;
    private ImageButton singer_back;
    String url;
    private String value;
    private View headView;

    /**
     * 通过value拼接网址
     **/
    public FragmentManSinger_jump(String value) {
        this.value = value;
        url = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.learn.detail"
                + Finnal_Base.query + "&value=" + value + "&desc=%E7%94%B7%E6%AD%8C%E6%89%8B";


    }

    /**
     * 初始化数据
     **/
    @Override
    protected void initData() {
        //判断value设置对应标题
        TextView viewById = (TextView) view.findViewById(R.id.singer_Title);
        if (value.equals("0")) {
            viewById.setText("男歌手");
        } else if (value.equals("1")) {
            viewById.setText("女歌手");
        } else {
            viewById.setText("乐队组合");
        }
        getData();
    }

    /**
     * 初始化控件
     **/
    @Override
    protected void initView() {

        singleton = VolleySingleton.getVolleySingleton(mActivity);
        manSinger_listView = (ListView) view.findViewById(R.id.manSinger_listView);
        manSinger_listView.addHeaderView(headView);
        manSinger_gridView = (NoScrollGridView) headView.findViewById(R.id.manSinger_gridView);
        singer_back = (ImageButton) view.findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);


    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_man_jump, null);
        headView = inflater.inflate(R.layout.jump_man_head, null);
        return view;
    }

    /**
     * 解析数据
     **/
    public void getData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                //解析实体类
                final SingerEntity entity = gson.fromJson(response, SingerEntity.class);


                //gridView 适配数据
                gridViewAdapter = new Fragment_Man_Singer_GridView_Adapter(mActivity, entity);
                manSinger_gridView.setAdapter(gridViewAdapter);
                manSinger_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String str = entity.getResult().getItems().get(position).getArtist_id();
                        String name = entity.getResult().getItems().get(position).getArtist_name();
                        //行布局点击跳转
                        FragmentSingerDetails singerDetails = new FragmentSingerDetails(str, name);
                        new ReplaceFragment(singerDetails, getActivity()
                                .getSupportFragmentManager(), R.id.fl_content, true, null);


                    }
                });
                //ListView 适配数据
                adapter = new Fragment_man_ListView_Adapter(mActivity, entity);
                manSinger_listView.setAdapter(adapter);
                manSinger_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String str = entity.getResult().getItems().get(position - 1).getArtist_id();
                        String name = entity.getResult().getItems().get(position - 1).getArtist_name();
                        //行布局点击跳转
                        FragmentSingerDetails singerDetails = new FragmentSingerDetails(str, name);
                        new ReplaceFragment(singerDetails, getActivity()
                                .getSupportFragmentManager(), R.id.fl_content, true, null);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        //添加请求队列
        singleton.addQueue(request, "tag");
    }

    /***
     * 点击监听事件
     ***/
    @Override
    public void onClick(View v) {
        //返回上一级fragment
        getActivity().getSupportFragmentManager().popBackStack();
    }


}
